<%-- 
    Document   : CategorySelect
    Created on : 2 févr. 2010, 22:47:07
    Author     : JC
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="fr.uha.ensisa.kfetmanager.category.CustomerCategoryManagerRemote,
        fr.uha.ensisa.kfetmanager.category.CustomerCategory,
        javax.naming.*,
        java.util.*"%>
<%!    private CustomerCategoryManagerRemote categoryManager = null;
    private boolean initializationSuccess = false;

    public void jspInit() {
        InitialContext ctxt = null;
        String name = CustomerCategoryManagerRemote.class.getName();
        try {
            ctxt = new InitialContext();
        } catch (Exception e) {
            System.err.println("Naming exception caught on new InitialContext().");
            e.printStackTrace();
            return;
        }
        try {
            categoryManager = (CustomerCategoryManagerRemote) ctxt.lookup(name);
        } catch (Exception e) {
            System.err.println("Naming exception caught on lookup.");
            e.printStackTrace();
            return;
        }
        initializationSuccess = true;
    }
%>

<%
            if (initializationSuccess) {
                List<CustomerCategory> list;
                try {
                    list = categoryManager.getCategories();
                } catch (Exception e) {
                    this.getServletContext().setAttribute("errorMessage", "Erreur lors de la recherche de cat&eacute;gories : " + e.getMessage());
                    this.getServletContext().setAttribute("redirectTo", "/admin");
                    this.getServletContext().getRequestDispatcher("/error.jsp").forward(request, response);
                    return;
                }
                Iterator<CustomerCategory> it = list.iterator();
%>
<h2>Administration cat&eacute;gories</h2>
<div>
    <table border="0">
        <%
                    while (it.hasNext()) {
                        CustomerCategory c = it.next();
        %>
        <tr>
            <td>
                <a href="<%=getServletContext().getContextPath()%>/admin/categories/edit?id=<%=c.getId().toString()%>">
                    <%=c.getLabel()%>
                </a>
            </td>
        </tr>
        <%     }    // while(it.hasNext())
            } // if(initializationSuccess)%>
    </table>
    <hr/>
    <p><a href="<%=getServletContext().getContextPath()%>/admin/categories/create">Cr&eacute;er cat&eacute;gorie</a></p>
</div>